package com.redstar.expenses;

import java.util.Calendar;
import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class ExpenseList extends SherlockActivity {
	
	private DatabaseHelper db;
	private List<Expense> expenses;
	private ListView list;
	private ExpenseListAdapter adapter;
	private final int ADD_EXPENSE = 1;
	private Intent ModifyIntent;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        db = new DatabaseHelper(this);
        
        ModifyIntent = new Intent(this, ModifyExpense.class);
        
        list = (ListView)findViewById(R.id.list);

        expenses = db.getAllExpenses();
        
		adapter = new ExpenseListAdapter(this, expenses);
		list.setAdapter(adapter);
        list.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> a, View v, int pos,
					long l) {
				// TODO Auto-generated method stub
				ModifyIntent.putExtra("Expense", expenses.get(pos));
                startActivity(ModifyIntent);
			}
        	
        });
        registerForContextMenu(list);
       // list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
	}
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    	super.onCreateContextMenu(menu, v, menuInfo);
      if (v.getId()==R.id.list) {
        menu.setHeaderTitle("Remove Entry");
        menu.add(Menu.NONE, 1, 1, "Delete");
      }
    }  
    
    @Override
    public boolean onContextItemSelected(android.view.MenuItem item) {
      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
      db.deleteExpense(expenses.get(info.position).getID());
      expenses.remove(info.position);
      adapter.notifyDataSetChanged();
      Toast.makeText(getApplicationContext(), "Entry Deleted", Toast.LENGTH_SHORT).show();

      return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case ADD_EXPENSE:
                // app icon in action bar clicked; add expense
                Expense exp = new Expense(Calendar.getInstance(), this);
                ModifyIntent.putExtra("Expense", exp);
                startActivity(ModifyIntent);
                return true;
                
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(0,ADD_EXPENSE,1,"Add")
        .setIcon(R.drawable.ic_input_add)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		
        return true;
   }
    
    protected void onResume(){
    	super.onResume();
    	
        expenses = db.getAllExpenses();
		adapter = new ExpenseListAdapter(this, expenses);
		list.setAdapter(adapter);

    }
}